package com.sth.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: sitianhong
 * @Description: 消费者
 * @Since: create in 2021/6/19 0019 14:40
 */
public class Consumer {
    // 队列名称
    public static final String QUEUE_NAME = "hello";
    public static final String QUEUE_NAME2 = "hello_queue";

    // 发送消息
    public static void main(String[] args) throws Exception {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("192.168.25.128");
        factory.setUsername("admin");
        factory.setPassword("123456");

        // 创建连接
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        System.out.println("等待接收消息....");
        //推送的消息如何进行消费的接口回调
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(message);
        };
        //取消消费的一个回调接口 如在消费的时候队列被删除掉了
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("消息消费被中断");
        };

        /**
         * 消费者消费消息
         * 1.消费哪个队列
         * 2.消费成功之后是否要自动应答 true代表自动应答 false手动应答
         * 3.消费者未成功消费的回调
         */
        channel.basicConsume(QUEUE_NAME2, true, deliverCallback, cancelCallback);
    }
}
